package com.example.data_save;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.data_save.database.UserDbHelper;
import com.example.data_save.entity.User;
import com.example.data_save.util.ToastUtil;

public class SQLiteHelperActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText etName;
    private EditText etAge;
    private CheckBox ckMarried;
    private UserDbHelper userDbHelper;
    private User user = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_helper);

        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        ckMarried = findViewById(R.id.cK_marry);

        findViewById(R.id.btn_saveData).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = etName.getText().toString();
                String age = etAge.getText().toString();

                user = new User(name,
                        Integer.parseInt(age),
                        ckMarried.isChecked());
                userDbHelper.insert(user);
            }
        });
        findViewById(R.id.btn_deleteData).setOnClickListener(this);
        findViewById(R.id.btn_updateData).setOnClickListener(this);
        findViewById(R.id.btn_queryData).setOnClickListener(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        userDbHelper = UserDbHelper.getInstance(this);
        userDbHelper.openWriteConnect();
        userDbHelper.openReadConnect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        userDbHelper.close();
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.btn_queryData:
                break;
            case R.id.btn_updateData:
                break;
//            case R.id.btn_deleteData:
//                userDbHelper.deleteByName(name);
//                if (userDbHelper.deleteByName(name) > 0){
//                    ToastUtil.successShow(this,"删除成功");
//                }
//                break;

        }
    }
}